package com.rongxue.pojo;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 自定义响应结果类型
 */
public class ObjectResult {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    // 响应业务状态
    private String code;

    // 响应消息
    private String str;

    // 响应中的数据
    private Object data;

    public static ObjectResult build(String code, String str, Object data) {
        return new ObjectResult(code, str, data);
    }

    public static ObjectResult ok(Object data) {
        return new ObjectResult(data);
    }

    public static ObjectResult ok() {
        return new ObjectResult(null);
    }

    public ObjectResult() {

    }

    public static ObjectResult build(String code, String str) {
        return new ObjectResult(code, str, new ArrayList());
    }

    public ObjectResult(String code, String str, Object data) {
        this.code = code;
        this.str = str;
        this.data = data;
    }

    public ObjectResult(Object data) {
        this.code = "200";
        this.str = "OK";
        this.data = data;
    }

    @Override
    public String toString() {
        return "ObjectResult{" +
                "code='" + code + '\'' +
                ", str='" + str + '\'' +
                ", data=" + data +
                '}';
    }

    //    public Boolean isOK() {
//        return this.code == 200;
//    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 将json结果集转化为TaotaoResult对象
     *
     * @param jsonData json数据
     * @param clazz    TaotaoResult中的object类型
     * @return
     */
    public static ObjectResult formatToPojo(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                return MAPPER.readValue(jsonData, ObjectResult.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (clazz != null) {
                if (data.isObject()) {
                    obj = MAPPER.readValue(data.traverse(), clazz);
                } else if (data.isTextual()) {
                    obj = MAPPER.readValue(data.asText(), clazz);
                }
            }
            return build(jsonNode.get("code").toString(), jsonNode.get("str").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 没有object对象的转化
     *
     * @param json
     * @return
     */
    public static ObjectResult format(String json) {
        try {
            return MAPPER.readValue(json, ObjectResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Object是集合转化
     *
     * @param jsonData json数据
     * @param clazz    集合中的类型
     * @return
     */
    public static ObjectResult formatToList(String jsonData, Class<?> clazz) {
        try {
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (data.isArray() && data.size() > 0) {
                obj = MAPPER.readValue(data.traverse(),
                        MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
            }
            return build(jsonNode.get("code").toString(), jsonNode.get("str").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

}
